import * as moment from "moment";
import dTalkApi from "../core/dTalkApi";
import consumerRecoderModel from "../model/consumerRecoderModel";
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';

class ConsumerInfoService{
    constructor(){}

    //根据员工id查询客户信息
    async getConsumerInfoByEmployeeId(userid:string){
        let json:any=[];
        let data=await consumerRecoderModel.find({userid:userid,comment:1});
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let element = data[index];
                json.push({
                    id:element._id,//id
                    city:element.city,
                    cityType:element.cityType,
                    month:element.month,//月份
                    comment:element.comment,//说明
                    code:element.consumerCode,//客户编码
                    name:element.consumerName,//客户名称
                    consumerAddress:element.consumerAddress,//客户地址
                    type1:element.typeOne,//客户类型1
                    type2:element.typeTwo,//客户类型2
                    type3:element.typeThree,//客户类型3
                    type4:element.typeFour, //客户类型4
                    eployeeName:element.userName,//员工名字
                    jobnumber:element.jobnumber   //员工工号    
                })
            }
        }
        return json;
    }

    
    //查询某个月的所有的客户信息
    async checkAllConsumerInfo(){
        let json:any=[];
        let data=await consumerRecoderModel.find({comment:1});
        if(data.length){
            for (let index = 0; index < data.length; index++) {
                let element = data[index];//element=consumerInfo
                json.push({
                    id:element._id,//id
                    city:element.city,
                    cityType:element.cityType,
                    month:element.month,//月份
                    code:element.consumerCode,//客户编码
                    name:element.consumerName,//客户名称
                    type1:element.typeOne,//客户类型1
                    type2:element.typeTwo,//客户类型2
                    type3:element.typeThree,//客户类型3
                    type4:element.typeFour, //客户类型4
                    eployeeName:element.userName,//员工名字
                    jobnumber:element.jobnumber   //员工工号    
                })
            }
        }
        return json; 
    }
    
    //删除客户信息
    async deleteConsumer(idList:string){
        console.log('删除客户开始!!!!!!')
        for (let index = 0; index < idList.length; index++) {
            let element = idList[index];//element=id
            let a=await consumerRecoderModel.remove({_id:element,comment:1});  
        }
        console.log('删除客户结束!!!!!!')
        return {code:0}
    }

    //添加客户信息
    async addConsumer(res:any){
        let json:any={};
        let result:any='';
        let data=await consumerRecoderModel.findOne({consumerCode:res.consumerCode,comment:1});
        if(data !=null){
            result='该号客户已存在!';
            return 1;
        }else{
            result='添加新的客户!!!!!!!!!';
            let a=await consumerRecoderModel.create({
                sourceId: res.consumerCode,
                month:moment().format('MM'),
                city:res.city,
                cityType:res.cityType,
                consumerCode: res.consumerCode,
                consumerName: res.consumerName,
                typeOne:res.typeOne,//类型1
                typeTwo:res.typeTwo,//类型2
                typeThree:res.typeThree,//类型3
                typeFour:res.typeFour, //类型4
                typeFive:'',//类型5
                typeSix:'',//类型6
                typeSeven:'',//类型7
                typeEight:'',//类型8
                typeNine:'', //类型9
                typeTen:'', //类型10  
                userid:'',
                jobnumber:'',
                userName:res.employeeName,//员工姓名
                date:moment().format('YYYY-MM-DD'),
                comment:1,//说明  '1--导入的客户信息数据'与'2--拜访记录'  '3----添加的客户信息'
                timestamp:'',
                planDate:'',//拜访日期
                planType:'',//拜访类型
                planPurpose:'',//拜访计划的拜访目的
                consumerType:'',
                vistitWay:'',
                position:'',
                isChuchai:false,//是否出差
                picture:'',
                purpose:'',  //记录的拜访目的          
                talkingPerson:'',//洽谈姓名
                job:'',//洽谈职位
                connection:'',//联系方式
                visitResult:'',
                followMatter:'',
                cooperation:'' //合作意向  
            });
            if(a){
                json={
                    one:'添加客户数量:'+1,
                    two:result
                }
            }else{
                json={
                    one:'添加客户数量:'+0,
                    two:result
                }
            }
            return json;
        }
    }

    //通过客户类型查询客户信息----筛选功能
    async checkConsumerByType(type1:string){
        let json:any=[];
        if(type1=='客户类型1'||type1=='客户类型2'||type1=='客户类型3'||type1=='客户类型4'){
            json=await this.checkAllConsumerInfo();
        }else{
            let data1=await consumerRecoderModel.find({comment:1,"$or":[{typeOne:type1},{typeTwo:type1},{typeThree:type1},{typeThree:type1},{typeFour:type1}]});
            for (let index = 0; index < data1.length; index++) {
                let element = data1[index];
                json.push({
                    id:element._id,//id
                    city:element.city,
                    cityType:element.cityType,
                    comment:element.comment,
                    code:element.consumerCode,//客户编码
                    name:element.consumerName,//客户名称
                    type1:element.typeOne,//客户类型1
                    type2:element.typeTwo,//客户类型2
                    type3:element.typeThree,//客户类型3
                    type4:element.typeFour, //客户类型4
                    eployeeName:element.userName,//员工名字
                    jobnumber:element.jobnumber   //员工工号  
                });
            }
        }
        return json;
    } 

    //搜索功能----模糊搜索与筛选功能一起查询数据
    async   fuzzyCheck(type:string,character:string){
        // console.log('客户类型:'+type+'----月份:'+month+'----模糊搜索字段:'+character);
        let json:any=[];
        let data:any[]=[];
        if(type==''){
            data=await consumerRecoderModel.find({comment:1,consumerName:{$regex:character}});
        }else{
            data=await consumerRecoderModel.find({consumerName:{$regex:character},comment:1,"$or":[{typeOne:type},{typeTwo:type},{typeThree:type},{typeFour:type}]});
        }
        if(data.length){
            for (let index = 0; index < data.length; index++) {
                let it = data[index];
                json.push({
                    id:it._id,//id
                    city:it.city,
                    cityType:it.cityType,
                    code:it.consumerCode,//客户编码
                    name:it.consumerName,//客户名称
                    type1:it.typeOne,//客户类型1
                    type2:it.typeTwo,//客户类型2
                    type3:it.typeThree,//客户类型3
                    type4:it.typeFour, //客户类型4
                    comment:it.comment,
                    eployeeName:it.userName,//员工名字
                    jobnumber:it.jobnumber   //员工工号    
                });
            }
        }
        return json;   
    }

    //分配客户---可批量分配客户给一个员工
    //参数:consumerList:客户编码列表    userName:员工姓名
    async allocate_consumer(consumerList:any[],userId:string){
        let access_token=await dTalkApiUtil.getAccessToken();
        let employeeInfo=await dTalkApi.getUserDetail(userId,access_token);
        // let up=await consumerRecoderModel.update({consumerCode:{$in:consumerList}},{userid:userId,jobnumber:employeeInfo.jobnumber,userName:employeeInfo.name},{upsert:false,multi:true})
        for (let index = 0; index < consumerList.length; index++) {
            let element = consumerList[index];
            let b=  await consumerRecoderModel.update({consumerCode:element,comment:1},{userid:userId,jobnumber:employeeInfo.jobnumber,userName:employeeInfo.name},{upsert:true});
            // console.log(b,'bbbbbbbbbbbbbbbbbbbbbbbb');
        }
        // console.log(up,'<------------up')
    }
    
    ////筛选已分配与未分配的客户
    async checkUnallocatedConsumer(month:string,character:string){
        console.log('月份:'+month+'----字段:'+character);
        let json:any=[];
        let data1:any[]=[];
        if(character=='已分配'){
            data1=await consumerRecoderModel.find({comment:1,userName:{$ne:null}});
        }else if(character=='未分配'){
            data1=await consumerRecoderModel.find({comment:1,userName:''});
        }
        if(data1.length){
            for (let index = 0; index < data1.length; index++) {
                let element = data1[index];
                json.push({
                    id:element._id,//id
                    city:element.city,
                    cityType:element.cityType,
                    code:element.consumerCode,//客户编码
                    name:element.consumerName,//客户名称
                    type1:element.typeOne,//客户类型1
                    type2:element.typeTwo,//客户类型2
                    type3:element.typeThree,//客户类型3
                    type4:element.typeFour, //客户类型4
                    comment:element.comment,
                    eployeeName:element.userName,//员工名字
                    jobnumber:element.jobnumber   //员工工号    
                });      
            }
        }
        return json;
    }

    //模糊搜索与筛选一起----分配客户
    async fusszyCheckAllocateConsumer(character:string,type:string,month:string){
        // console.log('城市或销售姓名:'+type+'----月份:'+month+'----模糊搜索字段:'+character);
        let json:any[]=[];
        let data1:any[]=[]
        if(type !=''){
            data1=await consumerRecoderModel.find({consumerName:{$regex:character},comment:1,"$or":[{city:type},{userName:type}]});
        }else{
            data1=await consumerRecoderModel.find({consumerName:{$regex:character},comment:1});
        }
        if(data1.length){
            for (let index = 0; index < data1.length; index++) {
                let element = data1[index];
                json.push({
                    id:element._id,//id
                    city:element.city,
                    cityType:element.cityType,
                    code:element.consumerCode,//客户编码
                    name:element.consumerName,//客户名称
                    type1:element.typeOne,//客户类型1
                    type2:element.typeTwo,//客户类型2
                    type3:element.typeThree,//客户类型3
                    type4:element.typeFour, //客户类型4
                    comment:element.comment,
                    eployeeName:element.userName,//员工名字
                    jobnumber:element.jobnumber   //员工工号    
                });
            }
        }
        return json;  
    }

    //根据城市或员工名字筛选客户
    async selectConsumerByCityOrEmployeeName(character:string,month:string){
        // console.log('城市或销售姓名:'+character+'----月份:'+month);
        let json:any=[];
        let data=await consumerRecoderModel.find({comment:1,"$or":[{city:character},{userid:character}]});
        for (let index = 0; index < data.length; index++) {
            let element = data[index];
            json.push({
                id:element._id,//id
                city:element.city,
                cityType:element.cityType,
                code:element.consumerCode,//客户编码
                name:element.consumerName,//客户名称
                type1:element.typeOne,//客户类型1
                type2:element.typeTwo,//客户类型2
                type3:element.typeThree,//客户类型3
                type4:element.typeFour, //客户类型4
                comment:element.comment,
                eployeeName:element.userName,//员工名字
                jobnumber:element.jobnumber   //员工工号    
            });
        }  
        return json;
    }






}
let consumerInfo=new ConsumerInfoService();
export default consumerInfo;